package tgc.edu.demo.jkxt.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import tgc.edu.demo.jkxt.custom.AjaxResult;
import tgc.edu.demo.jkxt.custom.BaseController;
import tgc.edu.demo.jkxt.entity.Question;
import tgc.edu.demo.jkxt.entity.Shijuan;
import tgc.edu.demo.jkxt.form.QuestionForm;
import tgc.edu.demo.jkxt.service.QuestionService;
import tgc.edu.demo.jkxt.service.ShijuanService;
import tgc.edu.demo.jkxt.service.XuanXiangService;
@RestController
@RequestMapping(value = "/question")
@CrossOrigin
public class QuestionController extends BaseController<Question, Integer, QuestionForm>{
		@Autowired
		private QuestionService service;
		@Autowired
		private ShijuanService shijuanservice;
		@Autowired
		private XuanXiangService xuanxiangService;
		@Override
		public Object save(QuestionForm form) {
			Integer shijuanid = form.getShijuanid();
			Shijuan shijuan = shijuanservice.findById(shijuanid);
			Question model = new Question();
			Integer id = form.getId();
			if(id!=null) {
				model = service.findById(form.getId());
			}
			model.setTimu(form.getTimu());
			model.setType(form.getType());
			service.save(model);
			List<Question> list=new ArrayList<>();
			list.add(model);
			shijuan.setQuestion(list);
			shijuanservice.save(shijuan);
			return new AjaxResult(model);
		}
		@RequestMapping(value = "/findbyid")
		public Object findById(Integer id) {
			return new AjaxResult(service.findById(id));
		}
}
